package com.ruoyi.dailycheck.controller;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelWriter;
import com.alibaba.excel.write.metadata.WriteSheet;
import com.alibaba.fastjson2.JSON;
import com.github.pagehelper.PageInfo;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.ibatis.cursor.Cursor;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dailycheck.domain.EasDailycheck;
import com.ruoyi.dailycheck.service.IEasDailycheckService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 设备点检Controller
 * 
 * @author ruoyi
 * @date 2025-04-02
 */
@RestController
@RequestMapping("/dailycheck/dailycheck")
public class EasDailycheckController extends BaseController {


    @Autowired
    private IEasDailycheckService easDailycheckService;

    /**
     * 查询设备点检列表（滚动查询）
     */
    @PreAuthorize("@ss.hasPermi('dailycheck:dailycheck:list')")
    @GetMapping("/list")
    public AjaxResult list(EasDailycheck easDailycheck,
                           @RequestParam(required = false) String lastId,
                           @RequestParam(defaultValue = "20") int pageSize) {
        List<EasDailycheck> list = easDailycheckService.selectEasDailycheckListByScroll(easDailycheck, lastId, pageSize);
        Map<String, Object> result = new HashMap<>();
        result.put("list", list);
        result.put("hasMore", list.size() >= pageSize);
        if (!list.isEmpty()) {
            result.put("lastId", list.get(list.size() - 1).getID());
        }
        return AjaxResult.success(result);
    }
    /**
     * 获取设备点检详细信息
     */
    @PreAuthorize("@ss.hasPermi('dailycheck:dailycheck:query')")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") String ID) {
        return success(easDailycheckService.selectEasDailycheckByID(ID));
    }
    /**
     * 导出设备点检列表
     *//*
    @PreAuthorize("@ss.hasPermi('dailycheck:dailycheck:export')")
    @Log(title = "设备点检", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EasDailycheck easDailycheck) throws IOException {
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setHeader("Content-Disposition", "attachment;filename=dailycheck_export.xlsx");

        try (Cursor<EasDailycheck> cursor = easDailycheckService.selectEasDailycheckByCursor(easDailycheck)) {
            ExcelWriter excelWriter = EasyExcel.write(response.getOutputStream(), EasDailycheck.class).build();
            WriteSheet writeSheet = EasyExcel.writerSheet("设备点检数据").build();

            // 分批次写入（每1000条）
            int batchSize = 1000;
            List<EasDailycheck> batchList = new ArrayList<>(batchSize);

            for (EasDailycheck item : cursor) {
                batchList.add(item);
                if (batchList.size() >= batchSize) {
                    excelWriter.write(batchList, writeSheet);
                    batchList.clear();
                }
            }

            // 写入剩余数据
            if (!batchList.isEmpty()) {
                excelWriter.write(batchList, writeSheet);
            }

            excelWriter.finish();
        } catch (Exception e) {
            response.reset();
            response.setContentType("application/json");
            response.setCharacterEncoding("utf-8");
            response.getWriter().println(JSON.toJSONString(AjaxResult.error("导出Excel失败：" + e.getMessage())));
        }
    }*/



}